package com.sc.adminspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.adminspringboot.dao.IPostTagsDao;
import com.sc.adminspringboot.dao.IPostsDao;
import com.sc.adminspringboot.dao.IUserPreferencesDao;
import com.sc.adminspringboot.dto.TagInfo;
import com.sc.adminspringboot.pojo.*;
import com.sc.adminspringboot.service.*;
import com.sc.adminspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Carson
 * @since 2025-09-22 04:46:00
 */
@RestController
public class TagsController {
    @Autowired
    private ITagsService tagsService;
    @Autowired
    private IPostTagsService postTagsService;
    @Autowired
    private IUserPreferencesService userPreferencesService;
    @Autowired
    private IPostsService postsService;
    @Autowired
    private IUsersService usersService;

    @RequestMapping("/admin/tagPage")
    public ResponseMsg tagPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        IPage<Tags> pageData = new Page<>(page, size);
        pageData = tagsService.page(pageData);
        System.out.println(pageData);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, pageData);
    }

    @RequestMapping("/admin/tagAdd")
    public ResponseMsg tagAdd(@RequestBody TagInfo tagInfo) {

        String tagName = tagInfo.getTagName();
        Byte type = tagInfo.getType();
        Tags tags = new Tags();
        tags.setTagName(tagName);
        tags.setType(type);
        QueryWrapper<Tags> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_name", tagName).eq("type", type);
        if (tagsService.getOne(queryWrapper) != null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "标签已存在");
        }
        if (tagsService.save(tags)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "成功");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "失败");
        }

    }

    @RequestMapping("/admin/delTag")
    public ResponseMsg tagDelete(@RequestParam Integer tagId) {
        System.out.println(tagId);
        if (tagsService.removeById(tagId)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "成功");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "失败");
        }
    }

    @RequestMapping("/admin/tag/taglist")
    public ResponseMsg taglist(@RequestParam Integer type) {
        QueryWrapper<Tags> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, tagsService.list(queryWrapper));
    }

    @RequestMapping("/admin/tag/prefer-tags")
    public ResponseMsg preferTags(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        IPage<TagInfo> pageData = new Page<>(page, size);
        pageData = tagsService.preferTags(pageData);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, pageData);
    }

    @RequestMapping("/admin/tag/post-tags")
    public ResponseMsg postTags(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        IPage<TagInfo> pageData = new Page<>(page, size);
        pageData = tagsService.postTags(pageData);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, pageData);
    }

    @RequestMapping("/admin/tag/prefer-tags/add")
    public ResponseMsg preferTagsAdd(@RequestBody TagInfo info) {
        if (info.getTagId() == null || info.getRelatedId() ==  null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "参数错误");
        }
        Integer tagId = info.getTagId();
        Integer userId = info.getRelatedId();
        System.out.println(tagId);
        System.out.println(userId);
        QueryWrapper<Tags> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", tagId);
        if (tagsService.getOne(queryWrapper) ==  null){
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "标签不存在");
        }

        QueryWrapper<Users> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id", userId);
        if (usersService.getOne(queryWrapper1) == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "用户不存在");
        }
        UserPreferences userPreferences = new UserPreferences();
        userPreferences.setUserId(userId);
        userPreferences.setTagId(tagId);
        userPreferences.setClickTime(new Date(System.currentTimeMillis()));
        QueryWrapper<UserPreferences> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_id", userId).eq("tag_id", tagId);
        if (userPreferencesService.getOne(queryWrapper2) != null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "已存在");
        }
        if (userPreferencesService.save(userPreferences)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "成功");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "失败");
        }
    }
    @RequestMapping("/admin/tag/post-tags/add")
    public ResponseMsg postTagsAdd(@RequestBody TagInfo info) {
        System.out.println(info);
        if (info.getTagId() == null || info.getRelatedId() == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "参数错误");
        }

        Integer tagId = info.getTagId();
        Integer postId = info.getRelatedId();

        // 检查标签是否存在
        QueryWrapper<Tags> tagQueryWrapper = new QueryWrapper<>();
        tagQueryWrapper.eq("id", tagId);
        if (tagsService.getOne(tagQueryWrapper) == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "标签不存在");
        }
        QueryWrapper<Posts> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("post_id", postId);
        if (postsService.getOne(queryWrapper1) == null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "文章不存在");
        }
        // 创建文章标签关联
        PostTags postTags = new PostTags();
        postTags.setPostId(postId);
        postTags.setTagId(tagId);
        QueryWrapper<PostTags> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("post_id", postId).eq("tag_id", tagId);
        if (postTagsService.getOne(queryWrapper2) != null) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "已存在");
        }
        if (postTagsService.save(postTags)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "成功");
        } else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "失败");
        }
    }
    @RequestMapping("/admin/tag/prefer-tags/del")
    public ResponseMsg preferTagsDel(@RequestParam Integer id) {
        if (userPreferencesService.removeById(id)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "成功");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "失败");
        }
    }
    @RequestMapping("/admin/tag/post-tags/del")
    public ResponseMsg postTagsDel(@RequestParam Integer id) {
        if (postTagsService.removeById(id)) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "成功");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "失败");
        }
    }
}
